package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GxMessage;
import com.ruoyi.system.service.IGxMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息Controller
 * 
 * @author ruoyi
 * @date 2023-01-30
 */
@RestController
@RequestMapping("/system/message")
public class GxMessageController extends BaseController
{
    @Autowired
    private IGxMessageService gxMessageService;

    /**
     * 查询消息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GxMessage gxMessage)
    {
        startPage();
        List<GxMessage> list = gxMessageService.selectGxMessageList(gxMessage);
        return getDataTable(list);
    }

    @GetMapping("/list/isNotRead/{userId}")
    public AjaxResult isNotRead(@PathVariable("userId") Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        List<GxMessage> list = gxMessageService.selectGxMessageListIsNotRead(userId);
        ajax.put("isNotRead",list);
        return ajax;
    }

    @PutMapping("/updateAllIsRead/{userId}")
    public AjaxResult updateAllIsRead(@PathVariable("userId") Long userId)
    {
        return toAjax(gxMessageService.updateAllIsRead(userId));
    }

    /**
     * 导出消息列表
     */
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxMessage gxMessage)
    {
        List<GxMessage> list = gxMessageService.selectGxMessageList(gxMessage);
        ExcelUtil<GxMessage> util = new ExcelUtil<GxMessage>(GxMessage.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        return AjaxResult.success(gxMessageService.selectGxMessageByMessageId(messageId));
    }

    /**
     * 新增消息
     */
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxMessage gxMessage)
    {
        return toAjax(gxMessageService.insertGxMessage(gxMessage));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxMessage gxMessage)
    {
        return toAjax(gxMessageService.updateGxMessage(gxMessage));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(gxMessageService.deleteGxMessageByMessageIds(messageIds));
    }
}
